package com.example.autopia.view.Fragments

import android.content.Intent
import android.content.pm.ApplicationInfo
import android.content.pm.PackageManager
import android.os.Bundle
import android.util.Log
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.example.autopia.R
import com.example.autopia.databinding.FragmentMapBinding
import com.example.autopia.model.Repositories.HotNewsRepository
import com.example.autopia.model.Repositories.NewsRepository
import com.example.autopia.model.Repositories.ParkingRepository
import com.example.autopia.model.Repositories.TransactionRepository
import com.example.autopia.model.Repositories.UsersRepository
import com.example.autopia.utilities.TimeManager
import com.example.autopia.view.Activities.NewParkActivity
import com.example.autopia.view.Activities.ParkingListActivity
import com.google.android.gms.maps.CameraUpdateFactory
import com.google.android.gms.maps.GoogleMap
import com.google.android.gms.maps.SupportMapFragment
import com.google.android.gms.maps.model.LatLng
import com.google.android.gms.maps.model.MarkerOptions
import com.google.firebase.auth.ktx.auth
import com.google.firebase.ktx.Firebase

class MapFragment : Fragment() {

    private lateinit var binding: FragmentMapBinding
    private val firebaseAuth = Firebase.auth
    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?,
    ): View? {
        binding = FragmentMapBinding.inflate(inflater, container, false)
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        val rep = UsersRepository()
        val list =  rep.getAllItems()

        val man = TimeManager()
        Log.d("Yatoro",man.secondsToTime(29))

        binding.toParkingList.setOnClickListener {
            startActivity(Intent(requireActivity(),ParkingListActivity::class.java))
        }


        val repository = UsersRepository()
        val check = repository.getUserById(firebaseAuth.currentUser?.uid ?: "")
        check.observe(viewLifecycleOwner) { curUser ->
            if (curUser.admin==1){
                binding.toNewPark.visibility = View.VISIBLE
                binding.toNewPark.setOnClickListener {
                    startActivity(Intent(requireActivity(),NewParkActivity::class.java))
                }
            }
        }

        val latitude = 55.724584918118694// Ваша широта
        val longitude = 37.631852512779226 // Ваша долгота
        val latLng = LatLng(latitude, longitude)

        //Менеджер для карты
        val supportMapFragment = childFragmentManager.findFragmentById(R.id.map) as SupportMapFragment
        supportMapFragment.getMapAsync { googleMap ->
            val markerOptions = MarkerOptions()
                .position(latLng) // Установка позицию маркера на заданную широту и долготу
                .title("Моя метка") // Установка заголовок маркера (необязательно)

            // Создание маркера
            googleMap.addMarker(markerOptions)

            // Перемещение камеры с зумом
            val cameraUpdate = CameraUpdateFactory.newLatLngZoom(latLng, 17f)
            googleMap.moveCamera(cameraUpdate)
        }
    }

}